/**
 * @file index.js
 */

var EventEmitter = require('events').EventEmitter;
var app = require('./application');
var mixin = require('merge-descriptors');
var req = require('./request');
var res = require('./response');
var Route = require('./router/route')
var Router = require('./router')
var http = require('http');

exports = module.exports = createApplication;


function Application() {
    this.request = req;
    this.response = res;
    this.request.app = app;
    this.response.app = app;
}

mixin(Application.prototype, EventEmitter.prototype, false);
mixin(Application.prototype, app, false);


Application.prototype.listen = function () {
    var server = http.createServer(this.serverListen);
    return server.listen.apply(server, arguments);
};


Application.prototype.serverListen = function () {

};


function createApplication() {
    var app = new Application();
    app.init();

    app.serverListen = function (req, res, next) {
        app.handle(req, res, next);
    };

    return app;
}



/**
 * Expose the prototypes.
 */

exports.application = app;
exports.request = req;
exports.response = res;

/**
 * Expose constructors.
 */

exports.Route = Route;
exports.Router = Router;
